<?php

namespace app\api\model\points;

use app\common\model\points\Goods as GoodsModel;
use app\api\model\user\CouponLog as CouponLogModel;

/**
 * 积分商城商品模型
 * Class Goods
 * @package app\api\model\points
 */
class Goods extends GoodsModel
{
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'sales_initial',
        'sales_actual',
        'is_delete',
        'wxapp_id',
        'create_time',
        'update_time'
    ];

    /**
     * 商品详情：HTML实体转换回普通字符
     * @param $value
     * @return string
     */
    public function getContentAttr($value)
    {
        return htmlspecialchars_decode($value);
    }

    /**
     * 根据商品id集获取商品列表
     * @param $goodsIds
     * @param null $status
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getListByIds($goodsIds, $status = null)
    {
        $filter = ['goods_id' => ['in', $goodsIds]];
        $status > 0 && $filter['goods_status'] = $status;
        return $this->with(['category', 'image.file', 'sku', 'spec_rel.spec', 'delivery.rule'])
            ->where($filter)
            ->select();
    }

    /**
     * 获取商品列表
     * @param null $status
     * @param int $category_id
     * @param string $search
     * @param string $sortType
     * @param false $sortPrice
     * @param false $userInfo
     * @param int $goods_type
     * @param int $listRows
     * @return mixed|\think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($status = null, $category_id = 0, $search = '', $sortType = 'all', $sortPrice = false, $userInfo = false, $goods_type = -1, $listRows = 16)
    {
        // 获取商品列表
        $data = parent::getList($status, $category_id, $search, $sortType, $sortPrice, $userInfo, $goods_type, $listRows = 16);

        return $this->setPointsGoodsListDataFromApi($data, ['userInfo' => $userInfo]);
    }

    /**
     * 设置商品展示的数据 api模块
     * @param $data
     * @param array $param
     * @return mixed
     */
    private function setPointsGoodsListDataFromApi(&$data, $param)
    {
        // 计算并返回商品是否可以兑换
        //1 允许购买 2积分不足
        foreach ($data as $key => $value) {
            if ($value['goods_type']['value'] == 10) {
                $data[$key]['is_buy'] = $this->isBuyPointsGoods($param['userInfo'], $value['sku'][0]['goods_price']);
            } else {
                //验证返现劵是否可以购买
                $data[$key]['is_buy'] = $this->isBuyCouponGoods($param['userInfo'], $value['sku'][0]['goods_price']);
            }
        }
        return $data;
    }

    /**
     * 商品是否可以用返现劵兑换
     * @param $userInfo
     * @param $goods_price
     * @return int
     */
    private function isBuyCouponGoods($userInfo, $goods_price)
    {
        if (empty($userInfo)) {
            return 1;
        }

        $max_price = CouponLogModel::getUserMaxCoupon($userInfo['user_id']);
        if ($max_price >= $goods_price) {
            return 1;
        }
        return 2;
    }

    /**
     * 商品是否可以兑换
     * @param $userInfo
     * @param $goods_price
     * @return int
     */
    private function isBuyPointsGoods($userInfo, $goods_price)
    {
        if (empty($userInfo)) {
            return 1;
        }
        if (!empty($userInfo) && $userInfo['points'] >= $goods_price) {
            return 1;
        }
        return 2;
    }

}
